package web

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"testPaper/common"
	"testPaper/model"
	"testPaper/response"
)

// QueryCompany 查询公司用户
func QueryCompany(c *gin.Context) {
	DB := common.GetDB()
	pageIndex, _ := strconv.Atoi(c.PostForm("pageIndex"))
	pageSize, _ := strconv.Atoi(c.PostForm("pageSize"))
	name := c.PostForm("name")
	if pageSize == 0 || pageSize > 100 {
		pageSize = 20
	}
	if pageIndex <= 0 {
		pageIndex = 1
	}
	company := make([]model.Company, 0)
	var total int
	if name != "" {
		DB.Where("name LIKE ?", "%"+name+"%").Limit(pageSize).Offset((pageIndex - 1) * pageSize).Find(&company).Offset(-1).Limit(-1).Count(&total)
		response.Reponse(c, http.StatusOK, 200, gin.H{
			"data":     company,
			"total":    total,
			"page":     pageIndex,
			"pageSize": pageSize,
		}, "获取成功")
		return
	} else {
		DB.Limit(pageSize).Offset((pageIndex - 1) * pageSize).Find(&company).Offset(-1).Limit(-1).Count(&total)
		response.Reponse(c, http.StatusOK, 200, gin.H{
			"data":     company,
			"total":    total,
			"page":     pageIndex,
			"pageSize": pageSize,
		}, "获取成功")
		return
	}
}
